<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/2/20
 * Time: 11:31
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Level extends Base
{
    /**
     * 轮播图首页展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            extract(input());
            $where = [];
            if(isset($keyword)&&$keyword!="")
            {
                $where['level_name'] = ['like',"%" . $keyword . "%"];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('user_level')->where($where)->count();// 获取总条数
            $lists = Db::name('user_level')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('level_id asc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }

    /**
     * 添加banner
     * @return mixed|\think\response\Json
     */
    public function bannerAdd(){
        if(Request::instance()->isPost()){
            $banner_title = trim(input('banner_title'), ' ');
            $level_min = (int)input('level_min');
            $level_max = (int)input('level_max');
            if($level_min < 0 || $level_max < 0){
                return json(['code' => 100, 'msg' => '积分值不能小于0']);
            }
            if($level_min >= $level_max){
                return json(['code' => 100, 'msg' => '最小值不能大于最大值']);
            }
            $have = Db::table('up_user_level')
                ->where('level_min','<=',$level_min)
                ->where('level_max','>=',$level_min)
                ->find();
            if($have){
                return json(['code' => 100, 'msg' => '最小值在其他等级的范围内，请重新设置']);
            }
            $have = Db::table('up_user_level')
                ->where('level_min','<=',$level_max)
                ->where('level_max','>=',$level_max)
                ->find();
            if($have){
                return json(['code' => 100, 'msg' => '最大值在其他等级的范围内，请重新设置']);
            }
            $data = [
                'level_name' => $banner_title,
                'level_min' => $level_min,
                'level_max' => $level_max,
                'level_time' => date('Y-m-d H:i:s'),
            ];
            $res = Db::table('up_user_level')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch('level/banneradd');
    }

    /**
     * 编辑轮播图
     * @return mixed|\think\response\Json
     */
    public function bannerEdit(){
        if(Request::instance()->isPost()){
            $id = input('banner_id');
            $banner_title = trim(input('banner_title'), ' ');
            $level_min = (int)input('level_min');
            $level_max = (int)input('level_max');
            if($level_min < 0 || $level_max < 0){
                return json(['code' => 100, 'msg' => '积分值不能小于0']);
            }
            if($level_min >= $level_max){
                return json(['code' => 100, 'msg' => '最小值不能大于最大值']);
            }
            $have = Db::table('up_user_level')
                ->where('level_id','<>',$id)
                ->where('level_min','<=',$level_min)
                ->where('level_max','>=',$level_min)
                ->find();
            if($have){
                return json(['code' => 100, 'msg' => '最小值在其他等级的范围内，请重新设置']);
            }
            $have = Db::table('up_user_level')
                ->where('level_id','<>',$id)
                ->where('level_min','<=',$level_max)
                ->where('level_max','>=',$level_max)
                ->find();
            if($have){
                return json(['code' => 100, 'msg' => '最大值在其他等级的范围内，请重新设置']);
            }
            $data = [
                'level_name' => $banner_title,
                'level_min' => $level_min,
                'level_max' => $level_max,
                'level_time' => date('Y-m-d H:i:s'),
            ];
            $res = Db::table('up_user_level')->where('level_id', $id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $banner_id = input('banner_id');
        // banner
        $banner = Db::table('up_user_level')
            ->where('level_id', $banner_id)
            ->find();
        $this->assign([
            'q_id' => $banner_id,
            'banner' => $banner
        ]);
        return $this->fetch('level/banneredit');
    }

    /**
     * 上架
     * @return \think\response\Json
     */
//    public function bannerOn(){
//        $banner_id = input('id');
//        $res = Db::table('up_feedback_type')->where('feedback_type_id', $banner_id)->setField('feedback_type_status', 1);
//        if($res){
//            return json(['code' => 200, 'msg' => '上架成功']);
//        }else{
//            return json(['code' => 100, 'msg' => '上架失败']);
//        }
//    }

    /**
     * 下架
     * @return \think\response\Json
     */
//    public function bannerDown(){
//        $banner_id = input('id');
//        $res = Db::table('up_feedback_type')->where('feedback_type_id', $banner_id)->setField('feedback_type_status', 2);
//        if($res){
//            return json(['code' => 200, 'msg' => '下架成功']);
//        }else{
//            return json(['code' => 100, 'msg' => '下架失败']);
//        }
//    }
    /**
     * 删除
     * @return \think\response\Json
     */
    public function bannerDel(){
        $banner_id = input('id');
        $res = Db::table('up_user_level')->where('level_id', $banner_id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '成功']);
        }else{
            return json(['code' => 100, 'msg' => '失败']);
        }
    }
}